<template>
  <div class="index">
    <div class="header flex-justify-between">
      <div class="logo flex-align-center">
        <div class="logo-img"></div>
        <div class="theme">MJ智能画图</div>
      </div>
      <div class="register" @click="registerClick">立即注册</div>
    </div>
    <div class="banner-center flex-center">
      <div class="containter">
        <div class="flex-justify-between">
          <div class="left-register">
            <div class="left-register-img"></div>

            <div class="register-text-box">
              <div class="left-register-text">输入文字即可一秒出图 !</div>
              <div class="left-register-text">小白画图神器</div>
            </div>
            <div class="register-explain-box flex-column-justify-between">
              <div
                v-for="item in registerExplainList"
                :key="item.explainId"
                class="register-explain flex-align-center"
              >
                <div class="register-explain-icon"></div>
                <div class="register-explain-text">{{ item.text }}</div>
              </div>
            </div>
            <div class="register-btn flex-center" @click="registerClick">立即注册</div>
          </div>
          <div v-show="showLogin" class="right-login">
            <div class="login-title flex-center">
              <div class="login-title-img"></div>
              <div class="login-title-text">MJ智能画图</div>
            </div>
            <div class="login-from">
              <el-form ref="formRef" :model="form" size="small" :rules="rules">
                <div class="from-text">手机号</div>
                <el-form-item prop="phone">
                  <el-input
                    v-model="form.phone"
                    oninput="value=value.replace(/\D/g,'')"
                    maxlength="11"
                    class="input-box"
                  ></el-input>
                </el-form-item>
                <div class="from-text">验证码</div>
                <el-form-item class="validate-box" prop="validate">
                  <el-input
                    v-model="form.code"
                    oninput="value=value.replace(/\D/g,'')"
                    maxlength="6"
                    class="input-box"
                  >
                  </el-input>
                  <!-- 获取验证码 -->
                  <div v-if="canClick" class="get-validate flex-center" @click="getCode">
                    获取验证码
                  </div>
                  <!-- 倒计时 -->
                  <div v-if="!canClick" class="get-validate flex-center count-down">
                    {{ totalTime }}s
                  </div>
                </el-form-item>
                <div class="protocol flex">
                  <div class="protocol-checkbox">
                    <img
                      v-show="!checkboxValue"
                      class="check-img"
                      src="@/assets/images/index/check-unselected.png"
                      alt=""
                      @click="checkboxValue = true"
                    />
                    <img
                      v-show="checkboxValue"
                      class="check-img"
                      src="@/assets/images/index/check-selected.png"
                      alt=""
                      @click="checkboxValue = false"
                    />
                  </div>
                  <div class="protocol-text">
                    <div>
                      通过手机号验证码登录，未注册账号将自动注册。勾选即代表您阅读并同意<span
                        class="link-text"
                        @click="userAgreementClick"
                        >《用户协议》</span
                      >与<span class="link-text" @click="privacyAgreementClick">《隐私政策》</span>
                    </div>
                  </div>
                </div>
                <el-form-item>
                  <el-button type="primary" class="btn" @click="submitForm('formRef')"
                    >登录</el-button
                  >
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
        <div class="footer-box flex-center">
          <div class="footer flex-center">MJ中文网今天已生产{{ imageCount }}张图片</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus';
import { useRouter, useRoute } from 'vue-router';
const router = useRouter();
import { getCodeHttp, loginHttp, userInfoHttp } from '@/api/modules/common/index.js';
// showLogin
const showLogin = ref(false); //登录弹窗
const canClick = ref(true); //获取验证码按钮Flag
const totalTime = ref(60); //倒计时

const form = reactive({ phone: '', code: '' }); // 登录信息
const rules = reactive({
  phone: [],
  code: []
});
let checkboxValue = ref(false); // 协议勾选Flag
let imageCount = ref(getCurrentValue()); // 单独计算加数(每天24点重置,基础数字设定为:8947283，每3秒增加6)

const registerExplainList = [
  { explainId: '01', text: '注册即用，百分百原创图片' },
  { explainId: '02', text: '需求准，1秒生成高清大图！' }
];
onMounted(async () => {
  // 新用户进入首页留在首页，老用户在token未过期的情况下直接跳转使用页
  userInfoFun();
});
function userAgreementClick() {
  // let jumpUrl = window.location.origin + window.location.pathname + '#/userAgreement';
  let jumpUrl = window.global.apiURL + 'manager/agreement.html?id=1';
  window.open(jumpUrl);
}
function privacyAgreementClick() {
  // let jumpUrl = window.location.origin + window.location.pathname + '#/privacyAgreement';
  let jumpUrl = window.global.apiURL + 'manager/agreement.html?id=2';
  window.open(jumpUrl);
}
// 获取用户信息
function userInfoFun() {
  userInfoHttp()
    .then(res => {
      if (res.code === 0 && res.data) {
        if (res.data.token) {
          localStorage.setItem('aiToken', res.data.token);
        }
        localStorage.setItem('userInfo', JSON.stringify(res.data));
        router.push({
          path: '/use'
        });
      }
    })
    .catch(err => {
      if (err.code === -1) {
        showLogin.value = true;
      }
    });
}
// 跳转首页
function comebackIndex() {
  router.push({
    path: 'index'
  });
}
// 提交登录
function submitForm() {
  if (!form.phone || form.phone.length !== 11) {
    ElMessage.warning('请检查手机号');
    return;
  }
  if (!form.code) {
    ElMessage.warning('请输入验证码');
    return;
  }
  if (!checkboxValue.value) {
    ElMessage.warning('请勾选同意用户协议与隐私政策');
  } else {
    let data = {
      phone: form.phone,
      code: form.code
    };
    loginHttp(data).then(res => {
      if (res.code === 0 && res.data) {
        ElMessage.success('登录成功');
        localStorage.setItem('aiToken', res.data.token);
        localStorage.setItem('userInfo', JSON.stringify(res.data));
        router.push({
          path: '/use'
        });
      } else {
        ElMessage.warning(res.msg);
      }
    });
  }
}

// 点击注册
function registerClick() {
  showLogin.value = true;
}
// 获取验证码
function getCode() {
  if (!form.phone || form.phone.length !== 11) {
    ElMessage.warning('请检查手机号');
    return;
  }
  let data = {
    phone: form.phone
  };
  getCodeHttp(data).then(res => {
    if (res.code === 0) {
      ElMessage.success('发送成功');
      countDown();
    } else {
      ElMessage.warning(res.msg);
    }
  });
}

// 倒计时 并发送验证码
function countDown() {
  if (!canClick.value) return;
  canClick.value = false;
  let clock = window.setInterval(() => {
    totalTime.value--;
    if (totalTime.value < 1) {
      window.clearInterval(clock);
      totalTime.value = 60;
      canClick.value = true;
    }
  }, 1000);
  // getCode();
}

// 获取当前时间戳对应的值
function getCurrentValue() {
  const now = new Date();
  // 获取当天0点的Date对象
  const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
  // 计算从0点到当前经过的毫秒数
  const elapsed = now - today;
  // 转换为秒并计算3秒间隔的次数
  const intervals = Math.floor(elapsed / 3000); // 每3秒一个间隔
  return 8947283 + intervals * 6;
}
setInterval(() => {
  imageCount.value = getCurrentValue();
}, 3000);

// 示例用法
// console.log(getCurrentValue());
</script>

<style scoped lang="less">
.index {
  display: flex;
  flex-direction: column;
  width: 100%;
  min-width: 1200px;
  height: 100vh;
  min-height: 770px;
  background: url('@/assets/images/index/indexBG.png') no-repeat;
  background-size: cover;
  .header {
    height: 80px;
    padding: 10px 40px;
    box-sizing: border-box;
    background-color: transparent;

    .logo {
      color: #ffffff;
      .logo-img {
        width: 40px;
        height: 40px;
        background: url('@/assets/images/index/logo-index.png') no-repeat;
        background-size: cover;
        margin-right: 8px;
      }
      .theme {
        font-size: 20px;
      }
    }
    .register {
      width: 200px;
      height: 60px;
      line-height: 60px;
      text-align: center;
      background: url('@/assets/images/index/register-btn.png') no-repeat;
      background-size: cover;
      font-size: 20px;
      color: #f4f4f4;
    }
  }
  .banner-center {
    flex: 1;
  }
  .containter {
    width: 1000px;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    height: 560px;
    .left-register {
      width: 521px;
      height: 540px;
      padding-top: 50px;
      box-sizing: border-box;
      // background-color: #ccc;
      .left-register-img {
        width: 300px;
        height: 61px;
        background: url('@/assets/images/index/left-register-img.png') no-repeat;
        background-size: cover;
      }
      .register-text-box {
        height: 134px;
        margin-top: 21px;
        .left-register-text {
          font-size: 48px;
          color: #e8e8e8;
        }
      }
      .register-explain-box {
        margin-top: 52px;
        height: 84px;
        .register-explain {
          // height: 84px;
          .register-explain-icon {
            width: 18px;
            height: 27px;
            background: url('@/assets/images/index/left-register-icon.png') no-repeat;
            background-size: cover;
            margin: 0 5px 0 8px;
          }
          .register-explain-text {
            font-size: 24px;
            color: #e8e8e8;
          }
        }
      }
      .register-btn {
        margin-top: 52px;
        width: 240px;
        height: 72px;
        background: url('@/assets/images/index/register-btn-bg.png') no-repeat;
        background-size: cover;
        font-size: 20px;
        color: #ffffff;
        line-height: 28px;
      }
    }
    .right-login {
      box-sizing: border-box;
      width: 430px;
      height: 540px;
      margin-left: 20px;
      background-color: #ffffff;
      box-shadow: inset 0px 2px 6px 0px rgba(255, 255, 255, 0.5);
      border-radius: 48px;
      padding: 36px 18px;
      .login-title {
        margin-bottom: 24px;
        .login-title-img {
          width: 40px;
          height: 40px;
          background: url('@/assets/images/index/login-title-img.png') no-repeat;
          background-size: cover;
        }
        .login-title-text {
          font-size: 24px;
          color: #051019;
          font-weight: 700;
        }
      }
      .login-from {
        // background-color: #fdf;
        :deep(.el-form-item.is-error .el-input__wrappe) {
          box-shadow: 0 0 0 1px #eeeeee inset;
        }
        :deep(.el-input__wrapper) {
          background-color: #eee;
          border-radius: 20px;
        }
        :deep(.el-input--small .el-input__wrapper) {
          padding: 0;
        }
        :deep(.el-input) {
          --el-input-focus-border: 1px solid #fff;
          --el-input-focus-border-color: 1px solid #fff;
        }
        :deep(.el-input__inner) {
          height: 68px;
          font-size: 20px;
          padding-left: 24px;
          color: #333333;
        }

        .from-text {
          font-size: 16px;
          color: #000000;
          margin-bottom: 12px;
        }
        .input-box {
          width: 395px;
          height: 68px;
          background: #eeeeee;
          border-radius: 20px;
        }
        .validate-box {
          position: relative;
          .get-validate {
            position: absolute;
            right: 16px;
          }
        }
        .get-validate {
          width: 88px;
          height: 36px;
          background: #6053ff;
          border-radius: 18px;
          font-size: 13px;
          color: #ffffff;
        }
        .count-down {
          background: #a5a2a2;
        }
        .protocol {
          margin: 35px 0 48px;

          .protocol-checkbox {
            width: 20px;
            height: 20px;
            img {
              width: 20px;
              height: 20px;
            }
          }
          .protocol-text {
            font-size: 13px;
            color: #999999;
            margin-left: 10px;
            .link-text {
              color: #6053ff;
            }
          }
        }

        .btn {
          width: 348px;
          height: 60px;
          margin: 0 auto;
          background: url('@/assets/images/index/login-bg.png') no-repeat;
          background-size: cover;
          font-size: 20px;
          color: #ffffff;
          border: 0px;
        }
      }
    }
  }
  .footer-box {
    width: 100%;
    height: 62px;
    margin-top: 20px;
    .footer {
      width: 720px;
      height: 62px;
      background: linear-gradient(86deg, #050819 0%, rgba(96, 83, 255, 0) 100%);
      margin: 0 auto;
      border-radius: 31px;
      font-size: 18px;
      color: #e8e8e8;
    }
  }
}
</style>
